package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.wrap.EnableApiCallBack;
import com.xbongbong.paas.util.DataUtil;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.pro.enums.ApiCallBackEnum;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.domain.dao.competitor.CompetitorDao;
import com.xbongbong.saas.domain.entity.competitor.CompetitorEntity;
import com.xbongbong.saas.enums.OperateTypeEnum;
import com.xbongbong.saas.model.CompetitorModel;
import com.xbongbong.saas.model.CompetitorSubDataModel;
import com.xbongbong.saas.model.PaasFormSubDataModel;
import com.xbongbong.saas.util.StandardizeDataUtil;
import org.elasticsearch.action.support.WriteRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author gaojie
 * @version v1.0
 * @date 2022/8/10 16:44
 * @since v1.0
 */
@Service("competitorModel")
public class CompetitorModelImpl implements CompetitorModel {

    @Resource
    private CompetitorDao competitorDao;

    @Resource
    private PaasFormSubDataModel paasFormSubDataModel;

    @Resource
    private PaasEsModel paasEsModel;

    @Resource
    private CompetitorSubDataModel competitorSubDataModel;

    @Override
    public CompetitorEntity getByKey(Long id, String corpid) {
        CompetitorEntity competitor = competitorDao.getByKey(id, corpid);
        paasFormSubDataModel.findSubEntity(corpid, Collections.singletonList(competitor), IndexTypeEnum.IDX_SAAS_COMPETITOR_SUB);
        return competitor;
    }

    @Override
    public Integer updateBatch(List<UpdateDataEntity> updateDataEntityList, String corpid) throws XbbException {
        return updateBatch(updateDataEntityList,corpid,WriteRequest.RefreshPolicy.WAIT_UNTIL);
    }

    @Override
    public Integer updateBatch(List<UpdateDataEntity> updateDataEntityList, String corpid, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException {
        //获取子表单数据
        List<UpdateDataEntity> subDataList = DataUtil.getUpdateSubDataList(updateDataEntityList);
        //主表单删除子表单数据
        DataUtil.removeSubDataInData(updateDataEntityList);
        //批量更新表单数据
        Integer result = 0;
        if (CollectionsUtil.isNotEmpty(updateDataEntityList)) {
            StandardizeDataUtil.standardizeList(updateDataEntityList);
            result = competitorDao.updateBatch(updateDataEntityList,corpid);
            //批量更新es数据
            paasEsModel.updateBatchMuchField(updateDataEntityList,IndexTypeEnum.IDX_SAAS_COMPETITOR,refreshPolicy);
        }
        //子表单更新
        if (CollectionsUtil.isNotEmpty(subDataList)){
            competitorSubDataModel.updateBatch(subDataList,corpid);
        }
        return result;
    }

    @Override
    public Integer deleteBatch(List<Long> deleteIdList, String corpid) throws XbbException {
        //数据库批量删除
        Integer result = competitorDao.deleteByIdIn(corpid, deleteIdList);
        //es批量删除
        paasEsModel.deleteBatch(deleteIdList,corpid, DelEnum.DELETE.getDel(),IndexTypeEnum.IDX_SAAS_COMPETITOR);
        //批量删除子表单数据
        paasFormSubDataModel.deleteBatchByDataId(deleteIdList,corpid,IndexTypeEnum.IDX_SAAS_COMPETITOR_SUB,DelEnum.DELETE_SUBFORM);
        return result;
    }

    @Override
    public List<CompetitorEntity> getByKeys(Collection<Long> ids, String corpid) {
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid",corpid);
        map.put("idIn",ids);
        map.put("del",0);
        return competitorDao.findEntities(map);
    }

    @Override
    public Integer insert(CompetitorEntity entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        //获取子表单
        paasFormSubDataModel.transferSubForm(entity,IndexTypeEnum.IDX_SAAS_COMPETITOR_SUB);
        StandardizeDataUtil.standardizeNotString(entity);
        Integer success = competitorDao.insert(entity);
        PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
        BeanUtil.copyProperties(entity,paasFormDataEntity);
        //获取竞争对手es索引

        IndexTypeEnum indexTypeEnum = IndexTypeEnum.IDX_SAAS_COMPETITOR;
        InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, indexTypeEnum);
        paasEsModel.insert(insertDTO);
        paasFormSubDataModel.insertSubFormData(entity,IndexTypeEnum.IDX_SAAS_COMPETITOR_SUB);
        return success;
    }

    @Override
    public Integer update(CompetitorEntity entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        paasFormSubDataModel.transferSubForm(entity,IndexTypeEnum.IDX_SAAS_COMPETITOR_SUB);
        StandardizeDataUtil.standardizeNotString(entity);
        Integer success = competitorDao.update(entity);
        Integer updateSub = paasFormSubDataModel.updateSubFormData(entity, IndexTypeEnum.IDX_SAAS_COMPETITOR_SUB);
        if (Objects.equals(1,success) || Objects.equals(1,updateSub)){
            success = competitorDao.updateUpdateTime(now + 1, entity.getId(), entity.getCorpid());
            //更新es
            PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
            BeanUtil.copyProperties(entity, paasFormDataEntity, false);
            IndexTypeEnum indexTypeEnum = IndexTypeEnum.IDX_SAAS_COMPETITOR;
            InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, indexTypeEnum);
            insertDTO.getSource().remove("creatorId");
            paasEsModel.update(insertDTO);
        }
        //封装子表单数据
        if (Objects.nonNull(entity.getSubFormData()) && !entity.getSubFormData().isEmpty()){
            entity.getData().putAll(entity.getSubFormData());
        }

        return success;
    }

    @Override
    public List<CompetitorEntity> findEntitys(Map<String, Object> param) {
        List<CompetitorEntity> entities = competitorDao.findEntities(param);
        Object corpid = param.get(ParameterConstant.CORPID);
        if (Objects.nonNull(corpid)){
            paasFormSubDataModel.findSubEntity(corpid.toString(),entities,IndexTypeEnum.IDX_SAAS_COMPETITOR_SUB);
        }
        return entities;
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        Integer count = competitorDao.getEntitysCount(param);
        return count;
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        List<CompetitorEntity> entities = competitorDao.findEntities(param);
        return entities;
    }
}
